<!DOCTYPE HTML>
<html>
<head>
    <title>批量添加站外商品</title>
    <link rel="stylesheet" href="/stylesheets/style.css">
    <script src="/scripts/jquery.min.js"></script>
    <script src="/scripts/boot.js" type="text/javascript"></script>
    <link rel="stylesheet" href="/scripts/miniui/themes/default/miniui.css">
    <link rel="stylesheet" href="/scripts/miniui/themes/icons.css">
    <link rel="stylesheet" href="/scripts/miniui/themes/bootstrap/skin.css">
    <link rel="stylesheet" href="/editor/common.css">
    <script src="/scripts/miniui/miniui.js"></script>
</head>
<body>
<div id="form1" style="padding-left: 60px">
    <span style="width:60px;">商品分类：</span>
    <input id="btnSelectGoodsType" class="mini-buttonedit" onbuttonclick="onButtonSelectGoodsType"/>
    <input id="pid" name="pid" class="mini-textbox" required="true" type="number" style="width:155px;display: none;" readonly="readonly"/>
    <span style="width:60px;display:inline-block;">选择商品：</span>
    <input id="btnEdit1" class="mini-buttonedit" onbuttonclick="onButtonEdit" name="a" textName="b"/>
    <br/><br/>
</div>
<div>
    <a class="mini-button" iconCls="icon-remove" onclick="remove()">刪除</a>
</div>
<div id="datagrid1" class="mini-datagrid" style="width:100%;height:300px"
     idField="id" allowResize="true"
     sizeList="[10,30,50,100]" pageSize="10"
>
    <div property="columns">
        <div type="checkcolumn" width="20" ></div>
        <div field="objectId" width="0" align="center" headerAlign="center" >商品ID</div>
        <div field="SalesVolumeMonth"  width="0" align="center" headerAlign="center">月销量</div>
        <div field="sourceGoodsName" width="150" align="center" headerAlign="center">商品名称</div>
        <div field="sourceGoodsIcon"  width="50" align="center" headerAlign="center" renderer="imgHandle">商品Icon</div>
        <div field="price"  width="50" align="center" headerAlign="center">商品价格</div>
        <div field="commission"  width="50" align="center" headerAlign="center">佣金</div>
    </div>
</div>
<div style="margin: 30px 0;text-align: center">
    <a class="mini-button " iconCls="icon-ok" onclick="addScoreGoodShop()">确定</a>
    <a class="mini-button" iconCls="icon-cancel" onclick="onCancel">取消</a>
</div>
</body>
<script>
  function addScoreGoodShop(){
    var data = {}
    data.goodsTypeOId = mini.get("btnSelectGoodsType").getValue();
    data.goodsTypePId = mini.get("pid").getValue();
    let gridData = mini.get("datagrid1").getData()
    let goodsOIds = []
    for (let item of gridData){
      goodsOIds.push(item.objectId)
    }
    data.goodsOIds = goodsOIds
    // console.log(data)
    if (!data.goodsTypeOId){
      alert('请选择商品分类！')
      return
    }
    if (mini.get("datagrid1").getData().length < 1){
      alert('请选择商品！')
      return
    }
    $.ajax({
      type: "POST",
      url: "/getdata/addScoreGoodsShopForBatch",
      data:data,
      success: function (result) {
        if(result =="1"){
          alert("保存成功");
          CloseWindow("save");
        }else{
          alert("保存失敗")
          CloseWindow("save");
        }
      }
    })
  }
  function CloseWindow(action) {
    if (action == "close" && form.isChanged()) {
      if (confirm("数据被修改了，是否先保存？")) {
        return false;
      }
    }
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();
  }

  function onCancel(e) {
    CloseWindow("cancel");
  }

  function onButtonEdit(e) {
    var btnEdit = this;

    mini.open({
      url: "/editor/externalGoodsMultiChoice",
      title: "站外商品列表(多选)",
      width: 1100,
      height: 450,
      ondestroy: function (action) {
        if (action == "ok") {
          var iframe = this.getIFrameEl();
          var data = iframe.contentWindow.GetData();
          // console.log(data)
          data = mini.clone(data);    //必须
          if (data) {
            let grid = mini.get("datagrid1");
            grid.on("drawcell", function (e) {
              var  value = e.value;
              var  column = e.column;
              if (column.field == "sourceGoodsIcon") {
                if(value) {
                  e.cellHtml = '<img class="bigImg" onclick="bigImg()" src="' + value + '" width="30" height="30" alt="图片">';
                }else{
                  e.cellHtml = '<img class="bigImg" onclick="bigImg()"  src="http://7xiuo5.com1.z0.glb.clouddn.com/fgImage.png?imageMogr2/auto-orient/thumbnail/!100x100r/gravity/Center/crop/100x100/quality/80/auto-orient" width="30" height="30" alt="图片">';
                }
              }
            });
            grid.setData(data)
            grid.setTotalCount(data.length);
            btnEdit.setText("下表中查看商品");
          }
        }else {
          btnEdit.setText("未选择商品");
        }

      }
    });


  }
  function imgHandle(e) {
    var value = e.value;
    if(value==0){
      return "否";
    }
    return "是";
  }
  function onButtonSelectGoodsType(e) {
    var btnSelectGoodsType = this;
    mini.open({
      url: "/getdata/shopIntergationSelectGoodsType",
      showMaxButton: false,
      title: "选择商品分类",
      width: 350,
      height: 350,
      ondestroy: function (action) {
        if (action == "ok") {
          var iframe = this.getIFrameEl();
          var data = iframe.contentWindow.GetData();
          data = mini.clone(data);
          if (data) {
            // console.log(data)
            btnSelectGoodsType.setValue(data.id);
            btnSelectGoodsType.setText(data.text);
            mini.get('pid').setValue(data.pid);
          }
        }
      }
    });
  }
  function  remove() {
    let row = mini.get("datagrid1").getSelected();
    let grid = mini.get("datagrid1");
    if(row){
      let index =  grid.getData().indexOf(row)
      let dataForDel = grid.getData()
      dataForDel.splice(index, 1);
      grid.setData(dataForDel)
      grid.setTotalCount(dataForDel.length);
    }else {
      alert("请选中一条记录");
    }
  }
</script>
</html>